import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../ColorHelper.dart';
import '../log/LogUtil.dart';

class SelectorUtil{
  static FixedExtentScrollController? firstController;
  static FixedExtentScrollController? secondController;

  static List<String> hours = [];
  static List<String> mins = [];
  static int hourIndex = 0;
  static int minIndex = 0;
  static Widget buildTimeSelector(int hour, int min){
    if(hours.isEmpty){
      for(int i = 0; i < 24; i++){
        hours.add('$i'.padLeft(2, '0'));
      }
    }
    if(mins.isEmpty){
      for(int i = 0; i < 60; i++){
        mins.add('$i'.padLeft(2, '0'));
      }
    }
    return buildDubColumnSelector(hours, hour, mins, min, null);
  }

  static Widget buildDubColumnSelector(List list, int index, List list1, int index1, callback) {
    firstController = FixedExtentScrollController(initialItem: index);
    secondController = FixedExtentScrollController(initialItem: index1);
    hourIndex = index;
    minIndex = index1;
    return Container(
      width: 1.sw,
      height: 0.5.sh,
      color: ColorHelper.white,
      child: Column(
        children: [
          callback != null ? Container(
            decoration: BoxDecoration(
                border: Border(
                    bottom: BorderSide(width: 0.5.h, color: ColorHelper.grayEB)
                )
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                GestureDetector(
                  onTap:(){
                    Get.back();
                  },
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Text(
                      'cancel'.tr,
                      style: TextStyle(
                          color: ColorHelper.gray9B,
                          fontSize: 17.sp
                      ),
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: (){
                    Get.back();
                    if(callback != null){
                      callback(firstController?.selectedItem, secondController?.selectedItem);
                    }
                  },
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Text(
                      'confirm'.tr,
                      style: TextStyle(
                          color: ColorHelper.blue,
                          fontSize: 17.sp
                      ),
                    ),
                  ),
                )
              ],
            ),
          ) : const SizedBox.shrink(),
          Expanded(child: Row(
            children: [
              Expanded(
                child: CupertinoPicker.builder(
                  scrollController: firstController,
                  itemExtent: 50.0,
                  onSelectedItemChanged: (position){
                    LogUtil.println('first $position');
                    hourIndex = position;
                  },
                  itemBuilder: (ctx, index){
                    return Container(
                      height: 50.0,
                      alignment: Alignment.center,
                      child: Text(
                          list[index]
                      ),
                    );
                  }, childCount: list.length,),
              ),
              Expanded(
                child: CupertinoPicker.builder(
                  scrollController: secondController,
                  itemExtent: 50.0,
                  onSelectedItemChanged: (position){
                    LogUtil.println('second $position');
                    minIndex = position;
                  },
                  itemBuilder: (ctx, index){
                    return Container(
                      height: 50.0,
                      alignment: Alignment.center,
                      child: Text(
                          list1[index]
                      ),
                    );
                  }, childCount: list1.length,),
              )
            ],
          )),
        ],
      ),
    );
  }

  static FixedExtentScrollController? scrollController;

  static Widget buildSingleColumnSelector(List list, int index, callback) {
    scrollController = FixedExtentScrollController(initialItem: index);
    return Container(
      width: 1.sw,
      height: 0.5.sh,
      color: ColorHelper.white,
      child: Column(
        children: [
          Container(
            decoration: const BoxDecoration(
                border: Border(
                    bottom: BorderSide(width: 0.5, color: ColorHelper.grayEB)
                )
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                GestureDetector(
                  onTap:(){
                    Get.back();
                  },
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Text(
                      'cancel'.tr,
                      style: const TextStyle(
                          color: ColorHelper.gray9B,
                          fontSize: 17.0
                      ),
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: (){
                    if(callback != null){
                      callback(scrollController?.selectedItem);
                    }
                    Get.back();
                  },
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Text(
                      'confirm'.tr,
                      style: const TextStyle(
                          color: ColorHelper.blue,
                          fontSize: 17.0
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
          Expanded(child: CupertinoPicker.builder(
            scrollController: scrollController,
            itemExtent: 50.0,
            onSelectedItemChanged: (position){
              LogUtil.println(position);
            },
            itemBuilder: (ctx, index){
              return Container(
                height: 50.0,
                alignment: Alignment.center,
                child: Text(
                    '${list[index]}'.tr
                ),
              );
            }, childCount: list.length,))
        ],
      ),
    );
  }

  static FixedExtentScrollController? oneController;
  static FixedExtentScrollController? twoController;
  static FixedExtentScrollController? threeController;

  static Widget buildThreeSelector<T>(
      List<T> list, int index,
      List<T> list1, int index1,
      List<T> list2, int index2,
      {
        Function(T,T,T)? onConfirm,
        Function(int)? onOneChanged,
        Function(int)? onTwoChanged,
        Function(int)? onThreeChanged,
        bool noConfirmHeader = false,
        double? height
      }
      ) {
    LogUtil.println('index $index index1 $index1 index2 $index2');
    height = height??0.5.sh;
    oneController = FixedExtentScrollController(initialItem: index);
    twoController = FixedExtentScrollController(initialItem: index1);
    threeController = FixedExtentScrollController(initialItem: index2);
    return Container(
      width: double.infinity,
      height: height,
      color: ColorHelper.white,
      child: Column(
        children: [
          noConfirmHeader ? const SizedBox.shrink() : Container(
            decoration: BoxDecoration(
                border: Border(
                    bottom: BorderSide(width: 0.5.h, color: ColorHelper.grayEB)
                )
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                GestureDetector(
                  onTap:(){
                    Get.back();
                  },
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Text(
                      'cancel'.tr,
                      style: TextStyle(
                          color: ColorHelper.gray9B,
                          fontSize: 17.sp
                      ),
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: (){
                    Get.back();
                    if(onConfirm != null){
                      onConfirm.call(
                          list[oneController!.selectedItem],
                          list1[twoController!.selectedItem],
                          list2[threeController!.selectedItem]
                      );
                    }
                  },
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Text(
                      'confirm'.tr,
                      style: TextStyle(
                          color: ColorHelper.blue,
                          fontSize: 17.sp
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
          Expanded(child: Row(
            children: [
              Expanded(
                child: CupertinoPicker.builder(
                  scrollController: oneController,
                  itemExtent: 50.0,
                  onSelectedItemChanged: (position){
                    LogUtil.println('one $position');
                    if(onOneChanged != null){
                      onOneChanged.call(position);
                    }
                  },
                  itemBuilder: (ctx, index){
                    return Container(
                      height: 50.0,
                      alignment: Alignment.center,
                      child: Text(
                          '${list[index]}'.tr
                      ),
                    );
                  }, childCount: list.length,),
              ),
              Expanded(
                child: CupertinoPicker.builder(
                  scrollController: twoController,
                  itemExtent: 50.0,
                  onSelectedItemChanged: (position){
                    LogUtil.println('two $position');
                    if(onTwoChanged != null){
                      onTwoChanged.call(position);
                    }
                  },
                  itemBuilder: (ctx, index){
                    return Container(
                      height: 50.0,
                      alignment: Alignment.center,
                      child: Text(
                          '${list1[index]}'.tr
                      ),
                    );
                  }, childCount: list1.length,),
              ),
              Expanded(
                child: CupertinoPicker.builder(
                  scrollController: threeController,
                  itemExtent: 50.0,
                  onSelectedItemChanged: (position){
                    LogUtil.println('three $position');
                    onThreeChanged?.call(position);
                  },
                  itemBuilder: (ctx, index){
                    return Container(
                      height: 50.0,
                      alignment: Alignment.center,
                      child: Text(
                          '${list2[index]}'.tr
                      ),
                    );
                  }, childCount: list2.length,),
              )
            ],
          )),
        ],
      ),
    );
  }
}